/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-19
 * Time: 18:54
 */
public class Solution {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }
    }
    //方法一_不使用返回值
    public TreeNode invertTree(TreeNode root) {
        if (root == null) return null;
        TreeNode left = root.left;
        root.left = root.right;
        root.right = left;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    //方法二_使用返回值
    public TreeNode invertTree1(TreeNode root) {
        if (root == null) return null;

        TreeNode left = invertTree1(root.left);
        TreeNode right = invertTree1(root.right);

        root.left = right;
        root.right = left;
        return root;
    }
}
